"""
通用库
"""

from functools import wraps


# 登录认证装饰器(有参)
# 外层装饰器传参数，中层装饰器传函数，内层装饰器在调用被装饰函数前后添加功能逻辑
def auth(role):
    from core import admin, student, teacher
    def auth_login(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            if role == 'admin':
                if admin.user_dict['user']:
                    res = func(*args, **kwargs)
                    return res
                else:
                    admin.login()
            if role == 'student':
                if student.user_dict['user']:
                    res = func(*args, **kwargs)
                    return res
                else:
                    student.login()
            if role == 'teacher':
                if teacher.user_dict['user']:
                    res = func(*args, **kwargs)
                    return res
                else:
                    teacher.login()
        return wrapper
    return auth_login